﻿using Common;
using STMS.BLL;
using STMS.Models.VModels;
using STMS.WSTMSystem.FModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.Store
{
    public partial class FrmStoreRegionList : Form
    {
        public FrmStoreRegionList()
        {
            InitializeComponent();
        }

        StoreRegionBLL sRegionBLL = new StoreRegionBLL();
        private void btnStoreList_Click(object sender, EventArgs e)
        {
            FrmStoreList fStoreList = new FrmStoreList();
            Panel panel = this.Parent as Panel;
            panel.AddPanelPage(fStoreList);
        }

        private void FrmStoreRegionList1_Load(object sender, EventArgs e)
        {
            chkShowDel.Checked = false;
            cboStores.LoadStores();
            cboStatus.LoadTemperStatus();
            FindSRegionList();
        }

        /// <summary>
        /// 查询仓库分区列表
        /// </summary>
        private void FindSRegionList()
        {
            string keywords = txtKeyWords.Text.Trim();
            int storeId = cboStores.SelectedValue.GetInt();
            int temperState = cboStatus.SelectedValue.GetInt();
            bool showDel = chkShowDel.Checked;

            List<ViewStoreRegionInfo> regionList = sRegionBLL.GetStoreRegionList(storeId, temperState, keywords, showDel);

            if (regionList.Count > 0)
            {
                dgvStoreRegionList.AutoGenerateColumns = false;
                dgvStoreRegionList.DataSource = regionList;
                SetBtnsEnable(true);
                dgvStoreRegionList.ShowDgvBtnCols(showDel);
            }
            else
            {
                dgvStoreRegionList.DataSource = null;
                SetBtnsEnable(false);
            }
        }

        private void SetBtnsEnable(bool bl)
        {
            btnFind.Enabled = bl;
            btnDelete.Enabled = bl;
            dgvStoreRegionList.Visible = bl;
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            FindSRegionList();
        }

        private void chkShowDel_CheckedChanged(object sender, EventArgs e)
        {
            FindSRegionList();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ShowStoreRegionPage(1, 0);
        }

        /// <summary>
        /// 打开仓库分区信息页
        /// </summary>
        /// <param name="storeId"></param>
        private void ShowStoreRegionPage(int actType, int regionId)
        {
            FrmStoreRegionInfo fSRegionInfo = new FrmStoreRegionInfo();
            fSRegionInfo.Tag = new FInfoData()
            {
                ActType = actType,
                FId = regionId
            };
            fSRegionInfo.ReloadRegionList += FindSRegionList;
            fSRegionInfo.ShowDialog();
        }

        private void dgvStoreRegionList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewCell curCell = dgvStoreRegionList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                ViewStoreRegionInfo region = dgvStoreRegionList.Rows[e.RowIndex].DataBoundItem as ViewStoreRegionInfo;
                switch (curCell.FormattedValue.ToString())
                {
                    case "修改":
                        ShowStoreRegionPage(2, region.SRegionId);
                        break;
                    case "删除":
                        DeleteSRegion(region, 1);
                        break;
                    case "恢复":
                        DeleteSRegion(region, 0);
                        break;
                    case "移除":
                        DeleteSRegion(region, 2);
                        break;
                    default: break;
                }
            }
        }

        /// <summary>
        /// 删除/恢复/移除 分区信息
        /// </summary>
        /// <param name="region"></param>
        /// <param name="delType"></param>
        private void DeleteSRegion(ViewStoreRegionInfo region, int delType)
        {
            string delTypeName = "";
            string infoName = "仓库分区";
            switch (delType)
            {
                case 1: delTypeName = "删除"; break;
                case 0: delTypeName = "恢复"; break;
                case 2: delTypeName = "移除"; break;
            }
            string msgTitle = $"{delTypeName}{infoName}";
            if (MsgBoxHelper.MsgBoxConfirm(msgTitle, $"您确定要{delTypeName}该{infoName}信息吗？") == DialogResult.Yes)
            {
                //删除
                bool bl = false;
                switch (delType)
                {
                    case 1:
                        int re = sRegionBLL.LogicDeleteStoreRegion(region.SRegionId);
                        if (re == 2)
                        {
                            MsgBoxHelper.MsgErrorShow(msgTitle, $"{infoName}:{region.SRegionName} 已存储了产品，不能删除！");
                            return;
                        }
                        else if (re == 1)
                            bl = true;
                        else
                            bl = false;
                        break;
                    case 0:
                        bl = sRegionBLL.RecoverStoreRegion(region.SRegionId);
                        break;
                    case 2:
                        bl = sRegionBLL.DeleteStoreRegion(region.SRegionId);
                        break;
                }
                string sucText = bl ? "成功" : "失败";
                string msg = $"{infoName}：{region.SRegionName} {delTypeName}{sucText}！";
                if (bl)
                {
                    MsgBoxHelper.MsgBoxShow(msgTitle, msg);
                    FindSRegionList();
                }
                else
                {
                    MsgBoxHelper.MsgErrorShow(msgTitle, msg);
                    return;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<int> regionIds = new List<int>();
            foreach (DataGridViewRow row in dgvStoreRegionList.Rows)
            {
                ViewStoreRegionInfo region = row.DataBoundItem as ViewStoreRegionInfo;
                DataGridViewCheckBoxCell chkCell = row.Cells["colChk"] as DataGridViewCheckBoxCell;
                if (chkCell.FormattedValue.ToString() == "True")
                {
                    regionIds.Add(region.SRegionId);
                }
            }
            string msgTitle = "删除仓库分区";
            if (regionIds.Count > 0)
            {
                if (MsgBoxHelper.MsgBoxConfirm(msgTitle, "你确定要删除选择的仓库分区信息吗？") == DialogResult.Yes)
                {
                    string reDel = sRegionBLL.LogicDeleteStoreRegion(regionIds);
                    if (reDel != "" && reDel != "0")
                    {
                        MsgBoxHelper.MsgErrorShow(msgTitle, $"选择的仓库分区存储了产品，它们的编号是：{reDel}");
                        return;
                    }
                    else if (reDel == "")
                    {
                        MsgBoxHelper.MsgBoxShow(msgTitle, $"选择的仓库分区删除成功！");
                        FindSRegionList();
                    }
                    else
                    {
                        MsgBoxHelper.MsgErrorShow(msgTitle, $"选择的仓库分区删除失败！");
                        return;
                    }
                }
            }
            else
            {
                MsgBoxHelper.MsgErrorShow("删除仓库分区", "请选择要删除的仓库分区信息！");
                return;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            FrmStoreRegionList1_Load(this, e);
        }
    }
}
